<?php

namespace App\Http\Controllers\Api\gao;

use App\Common\Responses;
use App\Models\Store;
use App\Models\StoreComment;
use Illuminate\Http\Request;

/**
 * 门店管理
 */
class StoreController
{
    /**
     * 门店的多条件搜索、展示
     * gao
     * @param Request $request
     * @return false|string
     */
    public function index(Request $request){
        try {
            $params = $request->except('id');
            //多条件搜索
            if (!empty($params)){
                $where = [];
                foreach ($params as $k=>$v){
                    $where[] = ["$k","like","%$v%"];
                }
                $params = Store::selectStores($where);
                return Responses::success($params);
            }

            //如果传的是id就查详情
            $id = $request->get('id');
            if (!empty($id)){
                $params = Store::selectStores(['id'=>$id]);
                return Responses::success($params);
            }

            $params = Store::getStores();
            return Responses::success($params);

        }catch (\Exception $exception){
            return Responses::error([],0,$exception->getMessage());
        }

    }

    /**
     * 门店添加
     * gao
     * @param Request $request
     * @return false|string
     */
    public function create(Request $request){
        try {
            $params = $request->validate([
                'name' => 'required',
                'longitude' => 'required',
                'latitude' => 'required',
                'phone' => 'required',
                'year_price' => 'required',
                'month_price' => 'required',
            ]);
            $result = Store::addStore($params);
            if (!$result) throw new \Exception('未知错误，添加失败');
            return Responses::success($params);
        }catch (\Exception $exception){
            return Responses::error([],0,$exception->getMessage());
        }

    }

    /**
     * 门店修改
     * gao
     * @param Request $request
     * @return false|string
     */
    public function update(Request $request){
        try {
            $id = $request->post('id');
            $params = $request->validate([
                'name' => 'required',
                'longitude' => 'required',
                'latitude' => 'required',
                'phone' => 'required',
                'year_price' => 'required',
                'month_price' => 'required',
            ]);
            $result = Store::updateStore($id,$params);
            if (!$result) throw new \Exception('未知错误，修改失败');
            return Responses::success($params);

        }catch (\Exception $exception){
            return Responses::error([],0,$exception->getMessage());
        }
    }


    /**
     * 门店删除
     * gao
     * @param Request $request
     * @return false|string
     */
    public function del(Request $request){
        try {
            $id = $request->get('id');
            $result = Store::removeStore($id);
            if (!$result) throw new \Exception('未知错误，删除失败');
            return Responses::success($result);
        }catch (\Exception $exception){
            return Responses::error([],0,$exception->getMessage());
        }
    }


    /**
     * 查看评论
     * @param Request $request
     * @return false|string
     */
    function lookComments(Request $request){
        try {
            //门店id
            $id = (integer) $request->get('id');
            if (empty($id)) throw new \Exception('请问要查看谁的评论呢？');
            $test=StoreComment::AllComments();
            $data=$this->AllComments($test);
            dd($data);
            //返回数据
            return Responses::success($data);

        }catch (\Exception $exception){
            return Responses::error([],0,$exception->getMessage());
        }

    }

    function AllComments($data,$id=0,$arr=[]){
        foreach ($data as $item => $value) {
            if ($value['parent_id'] == $id){
                $value['child']=$this->AllComments($data,$value['id']);
                $arr[]=$value;
            }
        }
        return $arr;
    }

}
